package com.adcc.soundalarm.manager;

import com.adcc.soundalarm.config.regular.BGSFactory;
import com.adcc.soundalarm.entity.LogInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.*;

/**
 * Created by zhangdawei on 2017/3/9.
 */
public class SystemInfoManager {

    //构造方法私有化
    private SystemInfoManager(){}

    /**
     * 静态内部类
     */
    private static class LazyHolder{
        private static final SystemInfoManager INSTANCE = new SystemInfoManager();
    }

    /**
     * 单例方法
     * */
    public synchronized static SystemInfoManager getInstance() {
        return LazyHolder.INSTANCE;
    }

    // AMQS 名称
    private String amqsName;

    // MQ 名称
    private String mqName;

    // MQ 连接数状态
    private String mqConnectCountState;
    // MQ 连接数
    private String mqConnectCount;

    // GW 名称
    private String gwName;

    // AMQS 状态
    private String amqsState;

    // MQ 状态
    private String mqState;

    // GW 状态
    private String gwState;

    // GW 模式
    private String mode;

    private String host;

    private String port;

    private String maxconnectcount;

    // MQ 日志内容
    private List<String> log = Lists.newArrayList();

    // 用户
    private List<BGSFactory> bgsFactories = Lists.newArrayList();

    private Map<String,String> stateCache = Maps.newHashMap();

    private List<String> logCache = Lists.newArrayList();

    private List<LogInfo> logCacheForSearch = Lists.newArrayList();

    public String getAmqsName() {
        return amqsName;
    }

    public void setAmqsName(String amqsName) {
        this.amqsName = amqsName;
    }

    public String getMqName() {
        return mqName;
    }

    public void setMqName(String mqName) {
        this.mqName = mqName;
    }

    public String getGwName() {
        return gwName;
    }

    public void setGwName(String gwName) {
        this.gwName = gwName;
    }

    public String getAmqsState() {
        return amqsState;
    }

    public void setAmqsState(String amqsState) {
        this.amqsState = amqsState;
    }

    public String getMqState() {
        return mqState;
    }

    public void setMqState(String mqState) {
        this.mqState = mqState;
    }

    public String getGwState() {
        return gwState;
    }

    public void setGwState(String gwState) {
        this.gwState = gwState;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public List<String> getLog() {
        return log;
    }

    public void setLog(String log) {
        this.log.add(log);
    }

    public void setLog(List<String> log) {
        this.log.addAll(log);
    }

    public List<BGSFactory> getBgsFactories() {
        return bgsFactories;
    }

    public String getMqConnectCountState() {
        return mqConnectCountState;
    }

    public void setMqConnectCountState(String mqConnectCountState) {
        this.mqConnectCountState = mqConnectCountState;
    }

    public String getMqConnectCount() {
        return mqConnectCount;
    }

    public void setMqConnectCount(String mqConnectCount) {
        this.mqConnectCount = mqConnectCount;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getMaxconnectcount() {
        return maxconnectcount;
    }

    public void setMaxconnectcount(String maxconnectcount) {
        this.maxconnectcount = maxconnectcount;
    }

    public void setBgsFactories(Map<String,String> map) {
        this.bgsFactories = new ArrayList<>();
        if(map!=null){
            Set<Map.Entry<String,String>> set = map.entrySet();
            for(Iterator<Map.Entry<String,String>> iterator = set.iterator();iterator.hasNext();){
                Map.Entry<String,String> entry = iterator.next();
                BGSFactory bgsFactory = new BGSFactory();
                bgsFactory.setName(entry.getKey());
                bgsFactory.setCurrentState(entry.getValue());
                this.bgsFactories.add(bgsFactory);
            }
        }
    }

    public void clearAllState(){
        if(!Strings.isNullOrEmpty(this.amqsName)){
            this.amqsName = "";
        }
        if(!Strings.isNullOrEmpty(this.amqsState)){
            this.amqsState = "";
        }
        if(!Strings.isNullOrEmpty(this.gwName)){
            this.gwName = "";
        }
        if(!Strings.isNullOrEmpty(this.gwState)){
            this.gwState = "";
        }
        if(!Strings.isNullOrEmpty(this.mode)){
            this.mode = "";
        }
        if(!Strings.isNullOrEmpty(this.mqName)){
            this.mqName = "";
        }
        if(!Strings.isNullOrEmpty(this.mqState)){
            this.mqState = "";
        }
        if(!Strings.isNullOrEmpty(this.mqConnectCount)){
            this.mqConnectCount = "";
        }
        if(!Strings.isNullOrEmpty(this.mqConnectCountState)){
            this.mqConnectCountState = "";
        }
        if(bgsFactories!=null){
            bgsFactories.clear();
        }
    }

    /**
     * 清空日志
     */
    public void clearAllLog(){
        this.log.clear();
    }

    /**
     * 获取状态缓存
     * @return
     */
    public Map<String, String> getStateCache() {
        return stateCache;
    }

    /**
     * 清空缓存日志
     */
    public void clearAllLogInfoCache(){
        this.logCache.clear();
    }

    public List<String> getLogCache() {
        return logCache;
    }

    public void setLogCache(String logCache) {
        if(!this.logCache.contains(logCache)){
            this.logCache.add(logCache);
        }
    }

    public List<LogInfo> getLogCacheForSearch() {
        return logCacheForSearch;
    }

    public void setLogCacheForSearch(LogInfo log) {
        if(this.logCacheForSearch.size() > 100){
            this.logCacheForSearch.remove(0);
        }
        this.logCacheForSearch.add(log);
    }

    /**
     * 清空所有缓存
     */
    public void clearAllCache(){
        logCache.clear();
        stateCache.clear();
    }
}
